package com.xingxing.service.impl;

import com.xingxing.entity.UserEntity;
import com.xingxing.mapper.LogMapper;
import com.xingxing.mapper.UserMapper;
import com.xingxing.service.ConsumeService;
import com.xingxing.service.LogService;
import com.xingxing.service.UserService;
import com.xingxing.utils.CommUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: 小白一号
 * @date: 2022/11/18
 * @desc:
 */

@Transactional  //事务注解,自动开启事务
@Service
public class ConSumeServiceImpl implements ConsumeService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogMapper logMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private LogService logService;
    @Value("${water_card.pricePerSecond}")
    float pricePerSecond;

    @Override
    public boolean useWater(String userCardId, int seconds) {
        UserEntity user = userService.findByUserCardId(userCardId);
        if (user == null) {
            return false;
        }
        float cost = pricePerSecond * seconds;
        if (userService.updateBalance(userCardId, user.getUserCardBalance() - cost) > 0) {
            if (logService.updateLog(user.getUserCardId(), cost, CommUtils.getCurrentTime()) > 0) {
                return true;
            }
            return false;
        }
        return false;
    }
}
